/**
 * JSONRecordProperties.java
 *
 * Copyright (C) 2009,  Tomislav Milkovic
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package extjsdyntran.translation;

import java.util.Enumeration;
import java.util.Properties;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * @description Helper class used to create JSON objects
 */

class JSONRecordProperties {


	private static String meta = "{ idProperty: 'key', root: 'data', totalProperty: 'total', fields: [{name: 'key'},{name: 'value'}],sortInfo: {field: 'key', direction:'ASC'}}";
	
	public static String getJsonRecords(Properties props) throws Exception{
		StringBuffer sb = new StringBuffer();
		JSONObject jobj = new JSONObject();
		//jobj.put("metaData", new JSONObject(meta));
		JSONArray jarr = getRecords(props);
		
		jobj.put("data", jarr);
		jobj.put("total", jarr.length());
		jobj.put("success", true);
		return jobj.toString();
	}
	
	
	public static JSONArray getRecords(Properties props) throws Exception{
		Enumeration e = props.elements();
		Enumeration k = props.keys();
		JSONArray jarr = new JSONArray();
		int i = 0;
		while(e.hasMoreElements()){
			String val = (String)e.nextElement();
			String key = (String)k.nextElement();
			JSONObject jobj = new JSONObject();
			jobj.put("key", key);
			jobj.put("value", val);
			jarr.put(jobj);
		}
		return jarr;
	}
	
	
	public static JSONArray getArray(Properties props) throws Exception{
		Enumeration e = props.elements();
		Enumeration k = props.keys();
		JSONArray jarr = new JSONArray();
		int i = 0;
		while(e.hasMoreElements()){
			String val = (String)e.nextElement();
			String key = (String)k.nextElement();
			JSONArray jobj = new JSONArray();
			jobj.put( val);
			jobj.put( key);
			jarr.put(jobj);
		}
		return jarr;
	}

}
